home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / giochi / chinacha.lha / ChinaChallenge3 / C / cc3.c < prev    next >
C/C++ Source or Header  |  1994-05-08  |  11KB  |  495 lines

  1. /************************************************************************
  2. *                                    *
  3. * China Challenge III - the C Version                    *
  4. *                                    *
  5. * author    : Gunther Nikl                        *
  6. * created    : 8-may-94                        *
  7. * last change    : 8-may-94                        *
  8. *                                    *
  9. ************************************************************************/
  10.  
  11. /* our include file */
  12.  
  13. #include "cc3.h"
  14.  
  15. /* for compactness */
  16.  
  17. #define main _main
  18. #define exit _exit
  19.  
  20. /* init functions */
  21.  
  22. VOID InitMusic()
  23. {
  24.   ULONG rlen;
  25.   BPTR file;
  26.  
  27.   AudioPort.mp_Node.ln_Type=NT_MSGPORT;
  28.   if (!((BYTE)(AudioPort.mp_SigBit=AllocSignal(-1L))<0))
  29.   {
  30.     AudioPort.mp_SigTask=FindTask(NULL);
  31.     NEWLIST(&AudioPort.mp_MsgList);
  32.  
  33.     AudioIO.ioa_Request.io_Message.mn_Node.ln_Type=NT_MESSAGE;
  34.     AudioIO.ioa_Request.io_Message.mn_Node.ln_Pri=ADALLOC_MAXPREC;
  35.     AudioIO.ioa_Request.io_Message.mn_ReplyPort=&AudioPort;
  36.     AudioIO.ioa_Request.io_Flags=ADIOF_NOWAIT;
  37.     AudioIO.ioa_Data=ChannelMap;
  38.     AudioIO.ioa_Length=sizeof(ChannelMap);
  39.     if (!OpenDevice(AudioName,0,&AudioIO.ioa_Request,0))
  40.     {
  41.       AudioOpen++;
  42.       if ((SampleBuf=AllocMem(SAMPLESIZE,MEMF_CHIP|MEMF_PUBLIC)))
  43.       {
  44.         if ((file=Open(SampleName,MODE_OLDFILE)))
  45.         {
  46.           rlen=Read(file,SampleBuf,SAMPLESIZE);
  47.           Close(file);
  48.           if (rlen==SAMPLESIZE)
  49.           {
  50.             if (!(ciaa.ciapra & CIAF_LED))
  51.             {
  52.               ciaa.ciapra |= CIAF_LED; AudioOpen++;
  53.             }
  54.             OptMusic();
  55.             if (!AudioIO.ioa_Request.io_Error)
  56.               return;
  57.           }
  58.         }
  59.       }
  60.       FreeMusic();
  61.     }
  62.   }
  63.   MenuItems[7].Flags &= ~(CHECKED|ITEMENABLED|CHECKIT);
  64. }
  65.  
  66. LONG MakeGfx()
  67. {
  68.   PLANEPTR *planes;
  69.   int i;
  70.  
  71.   if ((ScrPtr=OpenScreen(&NewScreen)))
  72.   {
  73.     NewWindow.Screen=ScrPtr;
  74.     if ((WinPtr=OpenWindow(&NewWindow)))
  75.     {
  76.       SetMenuStrip(WinPtr,&MenuStrip[0]);
  77.       ShowTitle(ScrPtr,FALSE);
  78.       LoadRGB4(&ScrPtr->ViewPort,&ColorTab[0],1<<DEPTH);
  79.       InitRastPort(&RastPort);
  80.       SetFont(&RastPort,WinPtr->IFont);
  81.       InitBitMap(&BitMap,DEPTH,WIDTH,HEIGHT);
  82.       for (planes=&BitMap.Planes[0],i=DEPTH; i!=0 ; --i)
  83.        {
  84.          if (!(*planes++=AllocRaster(WIDTH,HEIGHT))) return;
  85.        }
  86.       RastPort.BitMap=&BitMap;
  87.       CurrentTime((ULONG *)&EntryTable[0],(ULONG *)&RandVal);
  88.       MakeDragon();
  89.       ScreenToFront(ScrPtr);
  90.       return 1;
  91.     }
  92.   }
  93.   /* return 0; */
  94. }
  95.  
  96. /* the main loop */
  97.  
  98. int main()
  99. {
  100.   volatile struct IntuiMessage *imsg;
  101.   ULONG imClass;
  102.   UWORD imCode;
  103.  
  104.   if (MakeGfx())
  105.   {
  106.     InitMusic();
  107.     for (;;)
  108.      {
  109.        if (!(imsg=(struct IntuiMessage *)GetMsg(WinPtr->UserPort)))
  110.          WaitPort(WinPtr->UserPort);
  111.        else
  112.        {
  113.          imClass = imsg->Class; imCode  = imsg->Code;
  114.          ReplyMsg(&imsg->ExecMessage);
  115.          DoIDCMP(imClass,imCode);
  116.          if (EndAll)
  117.            break;
  118.        }
  119.      }
  120.   }
  121.   FreeMusic();
  122.   CloseGfx();
  123.   return (0);
  124. }
  125.  
  126. /* cleanup functions */
  127.  
  128. VOID CloseGfx()
  129. {
  130.   PLANEPTR *planes,p;
  131.   int i;
  132.  
  133.   for (planes=&BitMap.Planes[0],i=DEPTH; i!=0 ; i--)
  134.    {
  135.      if ((p=*planes++))
  136.        FreeRaster(p,WIDTH,HEIGHT);
  137.    }
  138.   if (WinPtr)
  139.   {
  140.     ClearMenuStrip(WinPtr); CloseWindow(WinPtr);
  141.   }
  142.   if (ScrPtr)
  143.     CloseScreen(ScrPtr);
  144. }
  145.  
  146. VOID FreeMusic()
  147. {
  148.   if (AudioOpen)
  149.     CloseDevice(&AudioIO.ioa_Request);
  150.   if (SampleBuf)
  151.   {
  152.     FreeMem(SampleBuf,SAMPLESIZE); SampleBuf=NULL;
  153.     if (--AudioOpen)
  154.       ciaa.ciapra &= ~CIAF_LED;
  155.     AudioOpen=0;
  156.   }
  157. }
  158.  
  159. /* process all messages */
  160.  
  161. VOID DoIDCMP(ULONG imClass, ULONG imCode)
  162. {
  163.   LONG index,pos;
  164.  
  165.   if (imClass == MENUPICK)
  166.   {
  167.     ShowTitle(ScrPtr,FALSE);
  168.     for (;;)
  169.      {
  170.        SetAPen(&ScrPtr->RastPort,4);
  171.        Move(&ScrPtr->RastPort,0,0);
  172.        Draw(&ScrPtr->RastPort,WIDTH-1,0);
  173.        if ((UWORD)imCode == MENUNULL)
  174.          break;
  175.        switch (MENUNUM(imCode))
  176.        {
  177.          case 0: switch (ITEMNUM(imCode))
  178.                  {
  179.                    case 0: ProjectAbout(); break;
  180.                    case 1: EndAll=~0; break;
  181.                  }
  182.                  break;
  183.  
  184.          case 1: switch (ITEMNUM(imCode))
  185.                  {
  186.                    case 0: MakeDragon(); break;
  187.                    case 1: OptUndoMove(); break;
  188.                    case 2: OptUndoAll(); break;
  189.                    case 3: OptLoadDragon(); break;
  190.                    case 4: OptSaveDragon(); break;
  191.                    case 5: OptMusic(); break;
  192.                  }
  193.                  break;
  194.        }
  195.        imCode=(ULONG)(ItemAddress(&MenuStrip[0],imCode))->NextSelect;
  196.        if (EndAll)
  197.          break;
  198.      }
  199.   }
  200.   else
  201.     if ((imClass==MOUSEBUTTONS) && (imCode==IECODE_LBUTTON) && (pos=CheckPos())>=0)
  202.     {
  203.       if (TwoSelected && (PiecePos2 == pos || PiecePos1 == pos))
  204.       {
  205.         index=(MAXCOUNT-PieceCount)/2; PieceCount-=2;
  206.         NewDragon.PieceTable[PiecePos1] |= 0x80; NewDragon.UndoTable[index].pos1 = PiecePos1;
  207.         NewDragon.PieceTable[PiecePos2] |= 0x80; NewDragon.UndoTable[index].pos2 = PiecePos2;
  208.         ShowDragon();
  209.       }
  210.       else
  211.       {
  212.         if (OnePiece)
  213.         {
  214.           if (PiecePos1==pos)
  215.             return;
  216.           if (NewDragon.PieceTable[PiecePos1]==NewDragon.PieceTable[pos])
  217.           {
  218.             TwoSelected=1; PiecePos2=pos;
  219.           }
  220.           else
  221.           {
  222.             OnePiece=1; PiecePos1=pos;
  223.           }
  224.         }
  225.         else
  226.         {
  227.           OnePiece=1; PiecePos1=pos;
  228.         }
  229.         DrawImage(WinPtr->RPort,&Images[NewDragon.PieceTable[pos]],(TwoSelected ? 291:3),85);
  230.       }
  231.     }
  232. }
  233.  
  234. /* project function(s) */
  235.  
  236. VOID ProjectAbout()
  237. {
  238.   struct Window *wptr;
  239.   struct RastPort *rp;
  240.   struct About *p;
  241.   int i,j;
  242.  
  243.   AboutWindow.Screen=ScrPtr;
  244.   if ((wptr=OpenWindow(&AboutWindow)))
  245.   {
  246.     rp=wptr->RPort;
  247.     SetRast(rp,5); SetBPen(rp,5);
  248.     for (i=2; i!=0; i--)
  249.      { for (p=&About[0],j=9; j!=0; p++,j--)
  250.         {
  251.           SetAPen(rp,p->pens[i-1]); Move(rp,p->x+i-1,p->y+i-1); Text(rp,&p->text[0],23);
  252.         }
  253.        SetDrMd(rp,JAM1);
  254.      }
  255.     for (i=2; i!=0; i--)
  256.       DrawImage(rp,&Images[Random(30)+1],(i==2 ? 2:168),23);
  257.     WaitPort(wptr->UserPort);
  258.     CloseWindow(wptr);
  259.   }
  260. }
  261.  
  262. /* option functions */
  263.  
  264. VOID OptUndoMove()
  265. {
  266.   LONG index;
  267.  
  268.   if ((index=(MAXCOUNT-PieceCount)/2-1)>=0)
  269.   {
  270.     PieceCount+=2;
  271.     NewDragon.PieceTable[NewDragon.UndoTable[index].pos1] &= 0x7f;
  272.     NewDragon.PieceTable[NewDragon.UndoTable[index].pos2] &= 0x7f;
  273.     ShowDragon();
  274.   }
  275. }
  276.  
  277. VOID OptUndoAll()
  278. {
  279.   char *p;
  280.   short i;
  281.  
  282.   if (PieceCount!=MAXCOUNT)
  283.   {
  284.     PieceCount=MAXCOUNT;
  285.     for (p=&NewDragon.PieceTable[0],i=288; i!=0; *p++ &= 0x7f,i--) ;
  286.     ShowDragon();
  287.   }
  288. }
  289.  
  290. VOID OptLoadDragon()
  291. {
  292.   WORD header[2];
  293.   BPTR file;
  294.  
  295.   if ((file=ReqFile(0)))
  296.   {
  297.     Read(file,(APTR)&header[0],sizeof(header));
  298.     if (header[0]==0x4333)
  299.     {
  300.       PieceCount=header[1];
  301.       Read(file,&NewDragon,sizeof(struct Dragon));
  302.       ShowDragon();
  303.     }
  304.     Close(file);
  305.   }
  306. }
  307.  
  308. VOID OptSaveDragon()
  309. {
  310.   WORD header[2];
  311.   BPTR file;
  312.  
  313.   if ((file=ReqFile(1)))
  314.   {
  315.     header[0]=0x4333; header[1]=PieceCount;
  316.     Write(file,(APTR)&header[0],sizeof(header));
  317.     Write(file,&NewDragon,sizeof(struct Dragon));
  318.     Close(file);
  319.   }
  320. }
  321.  
  322. VOID OptMusic()
  323. {
  324.   struct IOAudio *areq;
  325.    BYTE OnOff;
  326.   UWORD cmd;
  327.  
  328.   if (AudioOpen)
  329.   {
  330.     OnOff=(MenuItems[7].Flags&CHECKED?~0:0);
  331.     if (Music!=OnOff)
  332.     {
  333.       areq=&AudioIO;
  334.       cmd=ADCMD_FINISH;
  335.       if (OnOff)
  336.       {
  337.         areq->ioa_Request.io_Flags=IOF_QUICK|ADIOF_PERVOL;
  338.         areq->ioa_Data=SampleBuf+104L;
  339.         areq->ioa_Length=2*51984;
  340.         areq->ioa_Period=428;
  341.         areq->ioa_Volume=55;
  342.         areq->ioa_Cycles=0;
  343.         cmd=CMD_WRITE;
  344.       }
  345.       areq->ioa_Request.io_Command=cmd;
  346.       BeginIO(&areq->ioa_Request);
  347.       Music=~Music;
  348.     }
  349.   }
  350. }
  351.  
  352. /* mouseclick valid ? */
  353.  
  354. LONG CheckPos()
  355. {
  356.   unsigned int x,y,z,d;
  357.   int i;
  358.  
  359.   for (d=1,i=3; i>=0; d+=3,i--)
  360.    {
  361.      if ( ((y=(WinPtr->MouseY-d)/30)<6) && ((x=(WinPtr->MouseX-d)/25)<12)
  362.           && (NewDragon.PieceTable[z=(6*12*i+12*y+x)]>0) )
  363.      {
  364.        if ( x==0 || x==11 || ((NewDragon.PieceTable[z-1]<=0 || NewDragon.PieceTable[z+1]<=0)
  365.             && (i==3 || NewDragon.PieceTable[12*6+z]<=0)))
  366.        {
  367.          return z;
  368.        }
  369.        break;
  370.      }
  371.    }
  372.   return -1;
  373. }
  374.  
  375. /* create a new dragon */
  376.  
  377. VOID MakeDragon()
  378. {
  379.   int i,j,k,l,pos;
  380.   UBYTE *p1,*p2;
  381.  
  382.   PieceCount=MAXCOUNT;
  383.  
  384.   for (p1=p2=&EntryTable[0],i=MAXCOUNT/4; i!=0; i--)
  385.    {
  386.      *p1++=i; *p1++=i; *p1++=i; *p1++=i;
  387.    }
  388.   for (p1=&NewDragon.PieceTable[0],j=-1,i=MAXCOUNT; i!=0; i--)
  389.    {
  390.      do
  391.      {
  392.       j++; k=(-8)&j; l=j-k; k=k>>3;
  393.      } while (!(PosTable[k]&1<<l));
  394.      pos=Random(i); p1[j]=p2[pos]; p2[pos]=p2[i-1];
  395.    }
  396.   ShowDragon();
  397. }
  398.  
  399. /* random number generator */
  400.  
  401. LONG Random(ULONG Num)
  402. {
  403.   ULONG i,j;
  404.  
  405.   i=RandVal; j=i>>12; i^=j; j=i<<20; i^=j; RandVal=i; return (i%Num);
  406. }
  407.  
  408. /* draw the dragon */
  409.  
  410. VOID ShowDragon()
  411. {
  412.   ULONG *p1;
  413.   BYTE *p2;
  414.   short n;
  415.   int i,j,k,x,y,d;
  416.  
  417.   TwoSelected=OnePiece=0;
  418.  
  419.   for (p1=&BackGroundTab[0],i=4; i!=0; i--)
  420.    {
  421.      DrawImage(&RastPort,&Images[0],p1[0],p1[1]); p1++; p1++;
  422.    }
  423.   DrawBorder(&RastPort,&Border[0],0,0);
  424.   for (p2=&NewDragon.PieceTable[0],d=10,i=4; i!=0; d-=3,i--)
  425.     for (y=0,j=6; j!=0; y+=30,j--)
  426.       for (x=0,k=12; k!=0; x+=25,k--)
  427.         if ((n=*p2++)>0)
  428.           DrawImage(&RastPort,&Images[n],x+d,y+d);
  429.   PrintPieces();
  430.   BltBitMapRastPort(&BitMap,0,0,WinPtr->RPort,0,0,WIDTH,HEIGHT-2,0xc0);
  431. }
  432.  
  433. /* print remaining pieces */
  434.  
  435. VOID PrintPieces()
  436. {
  437.   int i;
  438.  
  439.   for (i=3; i!=0; i--)
  440.    {
  441.      SetAPen(&RastPort,APenTab[i]);
  442.      RectFill(&RastPort,278-i,51-i,312+i,60+i);
  443.    }
  444.   sprintf(BlankStr,PieceFmt,PieceCount);
  445.   PrintIText(&RastPort,&MoveIText,0,0);
  446. }
  447.  
  448. /* select a file and open the file for read or write */
  449.  
  450. BPTR ReqFile(LONG Type)
  451. {
  452.   struct Library *ArpBase;
  453.   struct ChinaReq *req;
  454.   APTR oldwin,*wptr;
  455.   BPTR file=0;
  456.  
  457.   wptr=&((struct Process *)FindTask(NULL))->pr_WindowPtr;
  458.   oldwin=*wptr; *wptr=WinPtr; WinPtr->Flags |= RMBTRAP;
  459.   if ((req=(struct ChinaReq *)AllocMem(sizeof(struct ChinaReq),MEMF_CLEAR)))
  460.   {
  461.     if ((ArpBase=OpenLibrary(ArpName,39L)))
  462.     {
  463.       req->FReq.fr_Hail   = (!Type ? LoadDragonStr : SaveDragonStr);
  464.       req->FReq.fr_File   = &req->FileBuf;
  465.       req->FReq.fr_Dir    = &req->DirBuf;
  466.       req->FReq.fr_Window = WinPtr;
  467.       req->FReq.fr_Flags  = 0x28; /* DoColor && NewWindFunc */
  468.       req->FReq.fr_res1   = 1; /* LongPath */ 
  469.       req->FReq.fr_Func   = (VOID (*)())ChangeFunc;
  470.       if (FileRequest(ArpBase,&req->FReq))
  471.       {
  472.         TackOn(ArpBase,&req->DirBuf[0],&req->FileBuf[0]);
  473.         file=ArpOpen(ArpBase,&req->DirBuf[0],MODE_OLDFILE+Type);
  474.       }
  475.       CloseLibrary(ArpBase);
  476.     }
  477.     FreeMem((APTR)req,sizeof(struct ChinaReq));
  478.   }
  479.   WinPtr->Flags &= ~RMBTRAP; *wptr=oldwin;
  480.   return file;
  481. }
  482.  
  483. VOID ChangeFunc()
  484. {
  485.   register struct NewWindow *new asm("a0");
  486.  
  487.   new->LeftEdge=10; new->TopEdge=10;
  488. }
  489.  
  490. /************************************************************************
  491. *                                    *
  492. * end of China Challenge III - the C Version                *
  493. *                                    *
  494. ************************************************************************/
  495.